//存放交互逻辑的代码
//javascript 模块化

var seckill ={
	// fang封装秒杀相关的Ajax的url
	URL: {
		now : function() {
			return "/seckill/time/now";
		},
		exposer : function(seckillId) {
			return "/seckill/"+seckillId+"/exposer";
		},
		execution : function(seckillId,md5) {
			return "/seckill/"+seckillId+"/"+ md5 +"/execution";
		}
	},
	seckillHandler : function(seckillId,node){
		node.hide()
			.html('<button type="button" id="killBtn" class="btn btn-primary btn-lg">开始秒杀</button>');
		$.post(seckill.URL.exposer(seckillId),{},function(result){
			//在回调函数中执行交互流程
			if(result && result["success"]){
				var exposer = result["data"];
				if(exposer["exposed"]){
					//开启秒杀
					//获取秒杀地址
					var md5 = exposer["md5"]
					var killUrl = seckill.URL.execution(seckillId,md5);
					console.log("killUrl=" + killUrl); //TODO
					//one只绑定一次
					$("#killBtn").one("click", function() {
						//禁用秒杀按钮
						$(this).addClass("disable");
						//发送秒杀请求
						$.post(killUrl,{},function(result){
							if(result && result["success"]){
								var killResult = result["data"];
								var state = killResult["state"];
								var stateInfo = killResult["stateInfo"];
								//显示秒杀结果
								node.html('<span class="label label-success">'+ stateInfo +'</span>')
							}
							
						});
					});
					node.show();	
				}else{
					//未开始秒杀
					//获取时间,重新计算倒计时逻辑
					var now = exposer["now"];
					var start = exposer["start"];
					var end = exposer["end"];
					seckill.countDown(seckillId,start,end,now);
				}
			} else {
				console.log("result=" + result);
			}
		});
	},
	//计时方法
	countDown : function(seckillId,startTime,endTime,nowTime){
		var seckillBox = $("#seckillBox");
		//时间判断
		if(nowTime > endTime){
			//秒杀结束
			seckillBox.html("秒杀结束");
		} else if (nowTime < startTime) {
			//秒杀未开始
			var killTime = new Date(startTime + 1000);
			seckillBox.countdown(killTime, function(event) {
				var format = event.strftime("秒杀倒计时: %D天 %H时 %M分 %S秒");
				seckillBox.html(format);
			}).on("finish.countdown",function(){
				//倒计时完成回调方法
				//获取秒杀地址，控制按钮显示，执行秒杀逻辑
				seckill.seckillHandler(seckillId,seckillBox);
			});
		} else {
			//秒杀开始
			seckill.seckillHandler(seckillId,seckillBox);
		}
	},
	// 验证手机号
	validatePhone : function(phone) {
		// isNaN 表示非数字
		if(phone && phone.length == 11 && !isNaN(phone)){
			return true;
		} else {
			return false;
		}
	},
	detail:{
		// 详情页初始化
		init : function(params){
			// 手机验证和登录，记时交互
			// 规划我们的交互流程
			// 在cookie中查找秒杀手机号码
			var killPhone = $.cookie("killPhone");
			// 验证手机号
			if(!seckill.validatePhone(killPhone)){
				// 绑定手机号
				var killPhoneModal = $("#killPhoneModal");
				killPhoneModal.modal({
					// 显示弹出层
					show : true,
					// 禁止位置关闭
					backdrop : "static",
					// 禁止键盘关闭
					keyboard : false
				});
			}
			//绑定点击事件
			$("#killPhoneBtn").click(function(){
				var inputPhone = $("#killPhoneKey").val();
				if(seckill.validatePhone(inputPhone)){
					$.cookie("killPhone",inputPhone, {expires:7,path:"/seckill"});
					window.location.reload();
				} else {
					$("#killPhoneMessage").hide().html("<lablel class='label label-danger'>手机号错误！</label>").show(300);
				}
			});
			//已经登录
			//记时交互
			var seckillId = params.seckillId;
			var startTime = params.startTime;
			var endTime = params.endTime;
			$.get(seckill.URL.now(),{},function(result){
				if(result && result["success"]){
					var nowTime = result["data"];
					seckill.countDown(seckillId,startTime,endTime,nowTime);
				} else {
					console.log("result=" + result);
				}
			});
		}
	}
}